package co.fitstart.admin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.support.Page;
import com.demo2do.core.utils.StringUtils;

import co.fitstart.admin.dao.MarketCoursewareDao;
import co.fitstart.admin.service.MarketCoursewareService;
import co.fitstart.entity.market.CourseLevel;
import co.fitstart.entity.market.CreditRecord;
import co.fitstart.entity.market.MarketCourse;
import co.fitstart.entity.market.MarketCourseRound;
import co.fitstart.entity.market.MarketWechatImage;
import co.fitstart.entity.market.template.MarketBatchCoursewareTemplate;
import co.fitstart.entity.market.template.MarketCoursewareTemplate;
import co.fitstart.entity.market.template.MarketDailyCoursewareTemplate;
import co.fitstart.entity.market.template.MarketMonthlyCoursewareTemplate;
import co.fitstart.entity.user.User;

/**
 * 
 * @author dan
 *
 */
@Service("marketCoursewareService")
public class MarketCoursewareServiceImpl implements MarketCoursewareService{
    
    @Dynamic
    private MarketCoursewareDao marketCoursewareDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#listMarketCourse(com.demo2do.core.support.Page)
     */
    public List<MarketCourse> listMarketCourse(Page page) {
        
        List<MarketCourse> result = marketCoursewareDao.listMarketCourse(page.getBeginIndex(), page.getMaxResultRecords());
        
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#loadMarketCourse(java.lang.Long)
     */
    public MarketCourse loadMarketCourse(Long courseId) {
        return genericDaoSupport.load(MarketCourse.class, courseId);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#listMonthly(co.fitstart.entity.market.MarketCourse)
     */
    public List<MarketMonthlyCoursewareTemplate> listMonthly(MarketCourse marketCourse) {
        return marketCoursewareDao.listMonthlyCoursewareTemplate(marketCourse);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#createMarketCourse(co.fitstart.entity.market.MarketCourse)
     */
    public void createMarketCourse(MarketCourse marketCourse) {
        // 1. create marketCourse
        genericDaoSupport.save(marketCourse.initilization());
        
        // 2. create marketCourse's market wechat image
        MarketWechatImage marketWechatImage = new MarketWechatImage(marketCourse);
        genericDaoSupport.save(marketWechatImage);
        
        // 3. create market monthly courseware template
        for(int i=1; i<=marketCourse.getMonths(); i++) {
            String name = this.generateMonthlyName(marketCourse.getCourseLevel(), i);
            MarketMonthlyCoursewareTemplate monthlyTemplate = new MarketMonthlyCoursewareTemplate(name, i, i + "月课程", 4, marketCourse);
            genericDaoSupport.save(monthlyTemplate);
            
            // 4. create batch courseware template
            for(int j=1; j<=monthlyTemplate.getWeeks(); j++) {
                MarketBatchCoursewareTemplate batchTemplate = new MarketBatchCoursewareTemplate("第" + j + "周", j, j + "周课程", monthlyTemplate);
                genericDaoSupport.save(batchTemplate);
                
                // 5. create daily courseware template
                for(int k=1; k<8; k++) {
                    MarketDailyCoursewareTemplate dailyTemplate = new MarketDailyCoursewareTemplate("第" + k + "天", k, j+"周第"+k+"天课程", batchTemplate);
                    genericDaoSupport.save(dailyTemplate);
                }
            }
        }
    }
    
    /**
     * 
     * @param courseLevel
     * @param sorting
     * @return
     */
    private String generateMonthlyName(CourseLevel courseLevel, int sorting) {
        if(courseLevel.isPrimary() && sorting == 1) {
            return "初级";
        } else if ((courseLevel.isPrimary() && sorting < 4) || (courseLevel.isMiddle() && sorting < 3)) {
            return "中级";
        } else {
            return "高级";
        }
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#editMarketCourse(co.fitstart.entity.market.MarketCourse)
     */
    public void editMarketCourse(MarketCourse marketCourse) {
        genericDaoSupport.update(marketCourse);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#deleteMarketCourse(co.fitstart.entity.market.MarketCourse)
     */
    public void deleteMarketCourse(MarketCourse marketCourse) {
        // 1. list this course's all marketBatchCoursewareTemplate
//        List<MarketBatchCoursewareTemplate> batchTemplates = marketCoursewareDao.listMarketBatchCoursewareTemplate(marketCourse);
//        for(MarketBatchCoursewareTemplate batchTemplate : batchTemplates) {
//            
//            // 2. list marketDailyCoursewareTemplate by batch
//            List<MarketDailyCoursewareTemplate> dailyTemplates = marketCoursewareDao.listMarketDailyCoursewareTemplate(batchTemplate);
//            for(MarketDailyCoursewareTemplate dailyTemplate : dailyTemplates) {
//                // 3. delete marketCoursewareTemplate by daily
//                marketCoursewareDao.updateMarketCoursewareTemplate(dailyTemplate);
//                
//                // 4. delete this marketDailyCoursewareTemplate
//                genericDaoSupport.update(dailyTemplate.disable());
//            }
//            
//            // 5. delete this marketBatchCoursewareTemplate
//            genericDaoSupport.update(batchTemplate.disable());
//        }
//        
//        // 6. delete this marketCourse
//        genericDaoSupport.update(marketCourse.disable());
//        
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#loadMarketCourseRound(co.fitstart.entity.user.User)
     */
    public MarketCourseRound loadMarketCourseRound(User user) {
        return marketCoursewareDao.loadMarketCourseRound(user);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#loadMarketMonthlyCoursewareTemplate(co.fitstart.entity.market.MarketCourse, int)
     */
    public MarketMonthlyCoursewareTemplate loadMarketMonthlyCoursewareTemplate(MarketCourse course, int months) {
        return marketCoursewareDao.loadMarketMonthlyCoursewareTemplate(course, months);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#loadMarketBatchCoursewareTemplate(java.lang.Long)
     */
    public MarketBatchCoursewareTemplate loadMarketBatchCoursewareTemplate(Long batchId) {
        return genericDaoSupport.load(MarketBatchCoursewareTemplate.class, batchId);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#loadMarketBatchCoursewareTemplate(co.fitstart.entity.market.template.MarketMonthlyCoursewareTemplate, int)
     */
    public MarketBatchCoursewareTemplate loadMarketBatchCoursewareTemplate(MarketMonthlyCoursewareTemplate monthly, int weeks) {
        return marketCoursewareDao.loadMarketBatchCoursewareTemplate(monthly, weeks);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#editMarketBatchCoursewareTemplate(co.fitstart.entity.market.template.MarketBatchCoursewareTemplate)
     */
    public void editMarketBatchCoursewareTemplate(MarketBatchCoursewareTemplate batchCoursewareTemplate) {
        genericDaoSupport.update(batchCoursewareTemplate);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#loadMarketDailyCoursewareTemplate(java.lang.Long)
     */
    public MarketDailyCoursewareTemplate loadMarketDailyCoursewareTemplate(Long dailyId) {
        return genericDaoSupport.load(MarketDailyCoursewareTemplate.class, dailyId);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#loadMarketCoursewareTemplate(java.lang.Long)
     */
    public MarketCoursewareTemplate loadMarketCoursewareTemplate(Long coursewareId) {
        return genericDaoSupport.load(MarketCoursewareTemplate.class, coursewareId);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#editMarketDailyCoursewareTemplate(co.fitstart.entity.market.template.MarketDailyCoursewareTemplate)
     */
    public void editMarketDailyCoursewareTemplate(MarketDailyCoursewareTemplate dailyCoursewareTemplate) {
        genericDaoSupport.update(dailyCoursewareTemplate);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#createMarketCoursewareTemplate(co.fitstart.entity.market.template.MarketCoursewareTemplate, co.fitstart.entity.market.template.MarketDailyCoursewareTemplate)
     */
    public void createMarketCoursewareTemplate(MarketCoursewareTemplate coursewareTemplate,
            MarketDailyCoursewareTemplate dailyCoursewareTemplate) {
        // 1. create marketCoursewareTemplate
        genericDaoSupport.save(coursewareTemplate.initilization(dailyCoursewareTemplate));
        
        // 2. edit marketDailyCoursewareTemplate's actionAmount
        genericDaoSupport.update(dailyCoursewareTemplate.editActionAmount(1));
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#editMarketCoursewareTemplate(co.fitstart.entity.market.template.MarketCoursewareTemplate)
     */
    public void editMarketCoursewareTemplate(MarketCoursewareTemplate coursewareTemplate) {
        genericDaoSupport.update(coursewareTemplate);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#deleteMarketCoursewareTemplate(co.fitstart.entity.market.template.MarketCoursewareTemplate)
     */
    public void deleteMarketCoursewareTemplate(MarketCoursewareTemplate coursewareTemplate) {
        // 1. delete coursewareTemplate
        genericDaoSupport.update(coursewareTemplate.disable());
        
        // 2. edit marketDailyCoursewareTemplate's actionAmount
        genericDaoSupport.update(coursewareTemplate.getDaily().editActionAmount(-1));
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#listCreditRecord(co.fitstart.entity.user.User)
     */
    public List<CreditRecord> listCreditRecord(User user) {
        return marketCoursewareDao.listCreditRecord(user);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#getMarketWechatImage(co.fitstart.entity.market.MarketCourse)
     */
    public MarketWechatImage getMarketWechatImage(MarketCourse course) {
        return marketCoursewareDao.loadMarketWechatImage(course);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.MarketCoursewareService#editMonthlyCoursewareTemplates(java.util.List)
     */
    public void editMonthlyCoursewareTemplates(List<String> monthlies) {
        for(String monthly : monthlies) {
            String[] monthlyArray = StringUtils.split(monthly, "|");
            MarketMonthlyCoursewareTemplate template = genericDaoSupport.load(MarketMonthlyCoursewareTemplate.class, Long.parseLong(monthlyArray[0]));
            genericDaoSupport.update(template.editName(monthlyArray[1]));
        }
        
    }

}
